# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from dls import settings
import os
import scrapy
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from dls import db

class ImagePipeline(ImagesPipeline):
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield scrapy.Request(image_url)

    def item_completed(self, results, item, info):

        image_paths = [x['path'] for ok, x in results if ok]
        print image_paths
        if not image_paths:
            raise DropItem("Item contains no images")
        item['image_paths'] = image_paths
        title = item['title']
        content = item['content']
        img = item['image_paths'][0]
        img = img.split('/')[1]
        title = title.encode('utf-8')
        #db.insertArticle(title,img,content)
        return item
